<template>
  <view class="container" :style="appThemeStyle">
    <!-- 标题 -->
    <view class="page-title">提现申请</view>
    <!-- 表单组件 -->
    <view class="form-wrapper">
      <u-form :model="form" ref="uForm" label-width="140rpx">
        <u-form-item label="可用余额" >
          <u-input v-model="userInfo.bonus" placeholder="可用余额" disabled=""/>
        </u-form-item>
        <u-form-item label="提现金额" prop="amount">
          <u-input v-model="form.amount" placeholder="请输入提现金额,金额不得少于10元" />
        </u-form-item>
		<u-form-item label="开户银行" prop="bank_name">
		  <u-input v-model="form.bank_name" placeholder="例:中国农业银行" />
		</u-form-item>
		<u-form-item label="银行帐号" prop="bank_no">
		  <u-input v-model="form.bank_no" placeholder="请输入银行帐号" />
		</u-form-item>
		<u-form-item label="二级密码" prop="sec_password">
		  <u-input v-model="form.sec_password" placeholder="请输入二级密码" />
		</u-form-item>
        <u-form-item label="备注" prop="remark">
          <u-input v-model="form.remark" placeholder="备注" />
        </u-form-item>
		<u-form-item>
          <view style="color: red;">注：到账时间为第二个工作日；需要扣除10％的手续费、10%作为购物券。</view>
        </u-form-item>

      </u-form>
    </view>
    <!-- 操作按钮 -->
    <view class="footer">
      <view class="btn-wrapper">
        <view class="btn-item btn-item-main" :class="{ disabled }" @click="handleSubmit()">确定提现</view>
        <!-- <view class="btn-item btn-item-main disabled">确定提现</view> -->
      </view>
    </view>
  </view>
</template>

<script>
  import { isMobile } from '@/utils/verify'
  import * as UserApi from '@/api/user'

  // 表单验证规则
  const rules = {
    amount: [{
      required: true,
      message: '请输入提现金额,金额不得少于10元',
      trigger: ['blur', 'change']
    },{
      type: 'number',
      message: '提现金额只能是数字',
      trigger: ['blur', 'change']
    },{
		validator: function(rule, value, callback){
			if(value<1){
				  callback(new Error('提现金额必须大于10'))
			}
			callback()
		}, trigger: 'blur' 
	}],
    bank_name: [{
      required: true,
      message: '请输入开户银行',
      trigger: ['blur', 'change']
    }],
	bank_no: [{
      required: true,
      message: '请输入银行帐号',
      trigger: ['blur', 'change']
    }],
    sec_password: [{
      required: true,
      message: '请输入二级密码',
      trigger: ['blur', 'change']
    }],
    remark: [{
      max: 20,
      message: '最多20个字符',
      trigger: ['blur', 'change']
    }]
  }

  export default {
    components: {
    },
    data() {
      return {
        form: {
          amount: '',
          bank_name: '',
          bank_no: '',
          sec_password: '',
          remark: '',
		  type:'bonus_withdraw'
        },
        rules,
        // 按钮禁用
        disabled: false,
		userInfo:{}
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {},

    // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
    onReady() {
		this.$refs.uForm.setRules(this.rules)
	    this.getUserInfo()
    },

    methods: {
	// 获取当前用户信息
	getUserInfo() {
		const app = this
		return new Promise((resolve, reject) => {
			UserApi.info({}, { load: true })
			.then(result => {
			  app.userInfo = result.data.userInfo
			  resolve(app.userInfo)
			})
			.catch(err => {
			  if (err.result && err.result.status == 401) {
				resolve(null)
			  } else {
				reject(err)
			  }
			})
		})
	},

      // 表单提交
      handleSubmit() {
        const app = this
        if (app.disabled) {
          return false
        }
        app.$refs.uForm.validate(valid => {
          if (valid) {
            app.disabled = true
            UserApi.withdrawApply({userId:app.userInfo.user_id,data:app.form})
              .then(result => {
                app.$toast(result.message)
				setTimeout(() => {
				  uni.navigateBack()
				}, 120)
              })
              .finally(() => app.disabled = false)
          }
        })
      }

    }
  }
</script>

<style>
  page {
    background: #f7f8fa;
  }
</style>
<style lang="scss" scoped>
  .page-title {
    width: 94%;
    margin: 0 auto;
    padding-top: 40rpx;
    font-size: 28rpx;
    color: rgba(69, 90, 100, 0.6);
  }

  .form-wrapper {
    margin: 20rpx auto 20rpx auto;
    padding: 0 40rpx;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    border-radius: 16rpx;
    background: #fff;
  }

  .form-analysis {

    .analysis-foot {
      padding-bottom: 20rpx;

      .analysis-btn {
        float: right;
        background: linear-gradient(to right, $main-bg, $main-bg2);
        color: $main-text;
        border-radius: 30rpx;
        padding: 10rpx 30rpx;
        font-size: 26rpx;
      }
    }
  }

  // 底部操作栏
  .footer {
    margin-top: 80rpx;

    .btn-wrapper {
      height: 100%;
      // display: flex;
      // align-items: center;
      padding: 0 20rpx;
    }

    .btn-item {
      flex: 1;
      font-size: 28rpx;
      height: 86rpx;
      color: #fff;
      border-radius: 50rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .btn-item-wechat {
      background: #0ba90b;
      margin-bottom: 26rpx;
    }

    .btn-item-main {
      background: linear-gradient(to right, $main-bg, $main-bg2);
      color: $main-text;

      // 禁用按钮
      &.disabled {
        opacity: 0.6;
      }
    }

  }
</style>